<?php

/**
 * 钉钉
 * 钉钉微应用开发模块
 *
 * 参考：http://ddtalk.github.io/dingTalkDoc/#调用api接口
 *
 * @author:風 2016-03-17
 */
class DingTalk_Lite extends DingTalk_Service {

    protected $config;

    /**
     * @param string $config ['CORPID']  企业CORPID
     * @param string $config ['SECRET']  企业SECRET
     */
    public function __construct($config = NULL) {
        $this->config = $config;

        if ($this->config === NULL) {
            $this->config = DI()->config->get('app.DingTalk');
        }
    }

    /**
     * 获取部门列表
     * @return mixed
     */
    public function departmentList() {
        $response = $this->get("/department/list", [
            "access_token" => $this->getCorpAccessToken()
        ]);
        return $response['department'];
    }

    /**
     * 创建部门
     * @return mixed
     */
    public function departmentCreate($name, $parentid) {
        $response = $this->post("/department/create",
            [
                "access_token" => $this->getCorpAccessToken()
            ],
            [
                "name"     => $name,
                "parentid" => $parentid
            ]
        );
        return $response['id'];
    }

    /**
     * 更新部门信息
     * @param $deptId
     * @param $name
     * @return mixed
     */
    public function departmentUpdate($deptId, $name) {
        $response = $this->post("/department/update",
            [
                "access_token" => $this->getCorpAccessToken()
            ],
            [
                "name" => $name,
                "id"   => $deptId
            ]
        );
        return $response['id'];
    }

    /**
     * 删除部门
     * @param $deptId
     * @return bool
     */
    public function departmentDelete($deptId) {
        $this->get("/department/delete", [
            "access_token" => $this->getCorpAccessToken(),
            "id"           => $deptId
        ]);
        return true;
    }

    /**
     * 获取成员资料
     * @param $userid
     * @return mixed|string
     */
    public function userGet($userid) {
        $response = $this->get("/user/get", [
            "access_token" => $this->getCorpAccessToken(),
            "userid"       => $userid
        ]);
        unset($response['errcode']);
        unset($response['errmsg']);
        return $response;
    }

    /**
     * 创建成员
     * @param $name string 名字
     * @param $mobile string 手机号码
     * @param $department array 所属部门,字符串数组
     * @return mixed
     */
    public function userCreate($name, $mobile, $department) {
        $response = $this->post("/user/create",
            [
                "access_token" => $this->getCorpAccessToken()
            ],
            [
                "name"       => $name,
                "mobile"     => $mobile,
                "department" => $department
            ]
        );
        return $response;
    }

    /**
     * 更新成员资料
     * @param $userid
     * @param $name
     * @return mixed|string
     */
    public function userUpdate($userid, $name) {
        $response = $this->post("/user/update",
            [
                "access_token" => $this->getCorpAccessToken()
            ],
            [
                "name"   => $name,
                "userid" => $userid
            ]
        );
        return $response;
    }

    /**
     * 删除成员
     * @param $userid
     * @return mixed|string
     */
    public function userDelete($userid) {
        $response = $this->get("/user/delete",
            [
                "access_token" => $this->getCorpAccessToken(),
                "userid"       => $userid
            ]
        );
        return $response;
    }

    /**
     * 用户批量删除
     * @param $names 字符串数组，用户id
     * @return mixed|string
     */
    public function userBatchDelete($names) {
        $response = $this->post("/user/batchdelete",
            [
                "access_token" => $this->getCorpAccessToken(),
            ],
            [
                "useridlist" => $names
            ]
        );
        return $response;
    }

    /**
     * 获取部门成员
     * @param  $deptId 部门id
     * @return mixed|string
     */
    public function userSimpleList($deptId) {
        $response = $this->get("/user/simplelist",
            [
                "access_token"  => $this->getCorpAccessToken(),
                "department_id" => $deptId
            ]
        );
        return $response['userlist'];
    }

    /**
     * 获取部门成员（详情）
     * @param $deptId 部门id
     * @return mixed|string
     */
    public function userList($deptId) {
        $response = $this->get("/user/list",
            [
                "access_token"  => $this->getCorpAccessToken(),
                "department_id" => $deptId
            ]
        );
        return $response['userlist'];
    }

    /**
     * 获取用户信息
     * @param $code 用户临时授权码
     * @return mixed|string
     */
    public function UserInfo($code) {
        $code = $this->tmp_auth_code($code);
        $response = $this->get("/user/getuserinfo",
            [
                "access_token" => $this->getCorpAccessToken(),
                "code"         => $code['persistent_code']
            ]
        );
        return $response['userlist'];
    }


    /**
     * 发送消息给用户
     * @param $msgtype 消息类型 文本（text） 图片（image）语音（voice）文件（file）超链接（link）oa（oa）
     * @param $sender 发送者id
     * @param $cid 群消息或者个人聊天会话Id
     * @param $agentid
     * @param $content 消息体 详情参考http://ddtalk.github.io/dingTalkDoc/#发送消息到群会话
     * @return mixed
     */
    public function messageSend($msgtype, $sender, $cid, $agentid, $content = []) {
        $data = [
            "sender"  => $sender,
            "cid"     => $cid,
            "agentid" => $agentid,
            "msgtype" => $msgtype
        ];
        $data[$msgtype] = $content;

        $response = $this->POST("/message/send_to_conversation",
            [
                "access_token" => $this->getCorpAccessToken(),
            ],
            $data
        );
        return $response;
    }

    /**
     * 资源上传
     * @param $type
     * @param $media
     * @return mixed|string
     */
    public function mediaUpload($type, $media) {
        $response = $this->post("/media/upload",
            [
                "access_token" => $this->getCorpAccessToken(),
            ],
            [
                "type"  => $type,
                "meida" => $media
            ]
        );
        return $response;
    }

    /**
     * 资源获取
     * @param $mediaID 资源ID
     * @return mixed|string
     */
    public function mediaGet($mediaID) {
        $response = $this->get("/media/get",
            [
                "access_token" => $this->getCorpAccessToken(),
                "meida"        => $mediaID
            ]
        );
        return $response;
    }
}
